package com.xuda.springboot.controller;

import com.xuda.springboot.pojo.User;
import com.xuda.springboot.service.UserService;
import com.xuda.springboot.utils.VerifyCodeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author ：程序员徐大大
 * @description：TODO
 * @date ：2022-03-20 20:06
 */
@Controller
@RequestMapping(value = "user")
public class UserController {
    //日志
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    private UserService userService;
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }



    @RequestMapping(value = "/generateImageCode")
    public void generateImageCode(HttpSession session,HttpServletResponse response) throws IOException {
        //随机生成四位随机数
        String code = VerifyCodeUtils.generateVerifyCode(4);
        //保存到session域中
        session.setAttribute("code",code);
        //根据随机数生成图片，reqponse响应图片
        response.setContentType("image/png");
        ServletOutputStream os = response.getOutputStream();
        VerifyCodeUtils.outputImage(130,60,os,code);
    }

    /*
    用户注册
     */
    @RequestMapping(value = "/register")
    public String register(User user, String code, HttpSession session, Model model){
        log.debug("用户名：{}，真实姓名：{},密码：{},性别：{},",user.getUsername(),user.getRealname(),user.getPassword(),user.getGender());
        log.debug("用户输入的验证码：{}",code);
        try{
            //判断用户输入验证码和session中的验证码是否一样
            String sessioncode = session.getAttribute("code").toString();
           if(!sessioncode.equalsIgnoreCase(code)) {
               throw new RuntimeException("验证码输入错误");
           }
            //注册用户
            userService.register(user);
        }catch (RuntimeException e){
            e.printStackTrace();
            return "redirect:/register";//注册失败返回注册页面
        }
        return  "redirect:/login"; //注册成功跳转到登录页面
    }

    /**
     * 用户登录
     */
    @RequestMapping(value = "login")
    public String login(String username,String password,HttpSession session){
        log.info("本次登录用户名：{}",username);
        log.info("本次登录密码：{}",password);
        try {
            //调用业务层进行登录
            User user = userService.login(username, password);
            //保存Session信息
            session.setAttribute("user",user);
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/login";//登录失败回到登录页面
        }

        return "redirect:/employee/lists";//登录成功跳转到查询页面
    }

    //退出
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.invalidate();//session失效
        return "redirect:/login";//跳转到登录页面
    }
}
